<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button id="button1">点击按钮1</button>
    <button id="button2">点击按钮2</button>
    <button id="button3">点击按钮3</button>

    <script type="text/javascript">
        let button1 = document.getElementById('button1');
        let button2 = document.getElementById('button2');
        let button3 = document.getElementById('button3');

        let setCommand = function(button, command) {
            button.onclick = function() {
                command.execute();
            }
        }

        let MenuBar = {
            refresh: function() {
                console.log('MenuBar refresh');
            }
        }
        let SubMenu = {
            add: function() {
                console.log('sub add');
            },
            del: function() {
                console.log('sub del');
            }
        }

        const RefreshMenuBarCommand = function (receiver) {
            this.receiver = receiver;
        }
        RefreshMenuBarCommand.prototype.execute = function() {
            this.receiver.refresh();
        }

        const AddSubMenuCommand = function(receiver) {
            this.receiver = receiver;
        }
        AddSubMenuCommand.prototype.execute = function() {
            this.receiver.add();
        }

        const DelSubMenuCommand = function (receiver) {
            this.receiver = receiver;
        }
        DelSubMenuCommand.prototype.execute = function () {
            this.receiver.del();
        }

        const refreshMenuBarCommand = new RefreshMenuBarCommand(MenuBar);
        const addSubMenuCommand = new AddSubMenuCommand(SubMenu);
        const delSubMenuCommand = new DelSubMenuCommand(SubMenu);

        setCommand(button1, refreshMenuBarCommand);
        setCommand(button2, addSubMenuCommand);
        setCommand(button3, delSubMenuCommand);
    </script>
</body>
</html>